﻿using AppEszköz.Config;
using System;

namespace AppEszköz.Config
{
    public class ConfigRegistryHelper
    {
        private const string subKey1 = "SOFTWARE";
        private const string subKey2 = "CSAD";
        private SecureRegistry registry;

        public ConfigRegistryHelper(string applicationName, string settingsCategory)
        {
            string plainTextConfig1 = ConfigRegistryHelper.GetPlainTextConfig(applicationName, "Settings", "passPhrase");
            string plainTextConfig2 = ConfigRegistryHelper.GetPlainTextConfig(applicationName, "Settings", "saltValue");
            if (plainTextConfig1 == null || plainTextConfig2 == null)
                throw new MissingFieldException("PassPhrase and/or SaltValue could not be loaded from the registry");
            this.registry = new SecureRegistry(plainTextConfig1, plainTextConfig2, "SOFTWARE", "CSAD", applicationName, settingsCategory);
        }

        public ConfigRegistryHelper(string applicationName, string settingsCategory, string passPhrase, string saltValue)
        {
            ConfigRegistryHelper.SetPlainTextConfig(applicationName, "Settings", "passPhrase", passPhrase);
            ConfigRegistryHelper.SetPlainTextConfig(applicationName, "Settings", "saltValue", saltValue);
            this.registry = new SecureRegistry(passPhrase, saltValue, "SOFTWARE", "CSAD", applicationName, settingsCategory);
        }

        public static string GetConfig(string applicationName, string settingsCategory, string key)
        {
            return new ConfigRegistryHelper(applicationName, settingsCategory).GetConfig(key);
        }

        public string GetConfig(string key)
        {
            string str = string.Empty;
            try
            {
                return this.registry.ReadFromRegistry(key, true);
            }
            catch (Exception ex)
            {
                throw new MissingFieldException("Unable to read encrypted registry key: " + key, ex);
            }
        }

        public bool GetConfigBool(string key)
        {
            bool flag = false;
            string config = this.GetConfig(key);
            if (config != null)
            {
                if (config.Length != 0)
                {
                    try
                    {
                        flag = bool.Parse(config);
                    }
                    catch (FormatException ex)
                    {
                    }
                }
            }
            return flag;
        }

        public void SetConfig(string key, string value)
        {
            this.registry.WriteToRegistry(key, value, true);
        }

        private static string GetPlainTextConfig(string subKey3, string subKey4, string key)
        {
            string str = string.Empty;
            try
            {
                return new SecureRegistry("", "", "SOFTWARE", "CSAD", subKey3, subKey4).ReadFromRegistry(key, false);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to read registry key: SOFTWARE\\CSAD\\" + subKey3 + "\\" + subKey4 + "\\" + key, ex);
            }
        }

        private static void SetPlainTextConfig(string subKey3, string subKey4, string key, string value)
        {
            new SecureRegistry("", "", "SOFTWARE", "CSAD", subKey3, subKey4).WriteToRegistry(key, value, false);
        }
    }
}
